package org.example.m7_14;

import java.math.BigInteger;
import java.util.Stack;

public class Test3 {
    public static void main(String[] args) {
        String s = "11";
        String t = "99";
        BigInteger num1 = new BigInteger(s);
        BigInteger num2 = new BigInteger(t);
        System.out.println(num1.multiply(num2));
        System.out.println(new Test3().solve(s,t));
    }
    public String solve (String s, String t) {
        if(s == null || s.isEmpty()){
            return t;
        }
        if(t == null || t.isEmpty()){
            return s;
        }
        int r = 0;
        int sl = s.length();
        int tl = t.length();
        int l = sl > tl ? tl: sl;
        StringBuffer str = new StringBuffer();
        int i = 0;
        for (; i < l; i++) {
            int sNum = Integer.valueOf(s.charAt(sl - 1 - i)) - 48;
            int tNum = Integer.valueOf(t.charAt(tl - 1 - i)) - 48;
            int num = sNum * tNum + r;
            str.insert(0,num % 10);
            if (num > 9){
                r = num / 10;
            }else {
                r = 0;
            }
        }
        if (r == 0){
            if (sl == tl){
                return str.toString();
            } else if (sl > tl){
                multiply(i,str,s,sl);
            }else {
                multiply(i,str,t,tl);
            }
        }else if (r > 0){
            if (sl == tl){
                str.insert(0,r);
            } else if (sl > tl){
                multiply(i,r,str,s,sl);
            }else {
                multiply(i,r,str,t,tl);
            }
        }
        return str.toString();
    }
    private void multiply(int l,StringBuffer str,String s,int sl){
        while (l < s.length()){
            str.insert(0,s.charAt(s.length() - l));
            l++;
        }
    }
    private void multiply(int l,int r,StringBuffer str,String s,int sl){
        while (l < s.length()){
            int sNum = Integer.valueOf(s.charAt(sl - l)) - 48;
            int num = sNum + r;
            str.insert(0,num % 10);
            if (num > 9){
                r = num / 10;
            }else {
                r = 0;
            }
            l++;
        }
        if (r > 0){
            str.insert(0,r);
        }
    }
}
